package l1j.server.GameServer;

import java.util.HashMap;
import java.util.Map;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.Config;
import l1j.server.Thread.GeneralThreadPool;
import l1j.server.server.Shutdown;

/**
 * 監聽端口控制器<br>
 * 監聽端口啟動重置作業
 * @author dexc
 *
 */
public class EchoServerTimer extends TimerTask {

	private static final Logger _log = Logger.getLogger(EchoServerTimer.class.getName());

	private static EchoServerTimer _instance;

	private static final Map<Integer, ServerExecutor> _echoList = new HashMap<Integer, ServerExecutor>();

	public static EchoServerTimer get() {
		if (_instance == null) {
			_instance = new EchoServerTimer();
		}
		return _instance;
	}

	/**
	 * 重新啟動
	 */
	public void reStart() {
		try {
			if (Shutdown.SHUTDOWN) {
				return;
			}
			if (!_echoList.isEmpty()) {
				this.stopEcho();

				Thread.sleep(2000);

				this.startEcho();

			} else {
				_log.log(Level.SEVERE ,"監聽端口重置作業失敗(目前無任何監聽線程)!!");
			}

		} catch (final Exception e) {
			_log.log(Level.SEVERE ,e.getLocalizedMessage(), e);
		}
	}

	@Override
	public void run() {
		try {
			_log.info("監聽端口重置作業!");
			try {
				this.stopEcho();

				this.startEcho();

			} catch (final Exception e) {
				_log.log(Level.SEVERE ,"重新啟動端口作業失敗!!", e);
			}

			//System.gc();

		} catch (final Exception e) {
			_log.log(Level.SEVERE ,"監聽端口重置作業失敗!!", e);

		} finally {
			_log.info("監聽端口重置作業完成!!");
		}
	}

	/**
	 * 啟動監聽
	 */
	public void start() {
		try {
			if (_echoList.isEmpty()) {
				this.startEcho();
			}

		} catch (final Exception e) {
			_log.log(Level.SEVERE ,e.getLocalizedMessage(), e);

		} finally {
			if (Config.RESTART_LOGIN > 0) {
				_log.info("監聽端口重置作業啟動 間隔時間:" + Config.RESTART_LOGIN + "分鐘。");
				final int timeMillis = Config.RESTART_LOGIN * 60 * 1000;
				GeneralThreadPool.getInstance().aiScheduleAtFixedRate(this, timeMillis, timeMillis);
			}
		}
	}

	/**
	 * 啟用全部監聽
	 */
	public void startEcho() {
		try {
//			final String[] portList = Config.GAME_SERVER_PORT.split("-");
//			for (String ports : portList) {
//				int key = Integer.parseInt(ports);
//				ServerExecutor echoServer = new ServerExecutor(key);
//				if (echoServer != null) {
//					_echoList.put(new Integer(key), echoServer);
//					echoServer.stsrtEcho();
//				}
//				Thread.sleep(100);
//			}
			final ServerExecutor echoServer = new ServerExecutor(Config.GAME_SERVER_PORT);
			_echoList.put(new Integer(Config.GAME_SERVER_PORT), echoServer);
			echoServer.stsrtEcho();

		} catch (final Exception e) {
			_log.log(Level.SEVERE ,e.getLocalizedMessage(), e);
		}
	}

	/**
	 * 停止全部監聽
	 */
	public void stopEcho() {
		try {
			if (!_echoList.isEmpty()) {
				for (final Integer key : _echoList.keySet()) {
					final ServerExecutor echoServer = _echoList.get(key);
					if (echoServer != null) {
						echoServer.stopEcho();
					}
					Thread.sleep(100);
				}
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE ,e.getLocalizedMessage(), e);
		}
	}
}
